#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <poll.h>

int main(int argc, char *argv[]){
    if(argc != 2){
        printf("[%s]:Usage:%s <file>\n", argv[0], argv[1]);
        return -1;
    }

    int fdf = open(argv[1], O_RDWR);
    if(fdf < 0){
        perror("open");
        return -1;
    }
    
    int event, timeout = 5000;
    uint8_t button, value;
    struct pollfd fds = {
        .fd     = fdf,
        .events = POLLIN, 
    };

    while(1){
        int ret = poll(&fds, 1, timeout);
        if(ret == 1 && (fds.revents & POLLIN)){
            read(fdf, &event, sizeof(event));
            button = (event >> 8) & 0xff; 
            value = (event) & 0xff;
            printf("[%s]:button event : butto %u value %u\n",
                    argv[0], (unsigned int)button, (unsigned int)(value));
        }
        else
                    printf("[%s]time out\n",argv[0]);
    }

    close(fdf);
    return 0;
}